package Tanks;

import com.golden.gamedev.object.AnimatedSprite;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.collision.BasicCollisionGroup;
import com.golden.gamedev.object.sprite.VolatileSprite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author FRNeK
 */

public class Czolg extends AnimatedSprite
{
    public int _hp;
    public AnimatedSprite zycie1,zycie2,zycie3;
    public BufferedImage[] zycie1Img,zycie2Img,zycie3Img;
    protected double _speed;
    protected long _l=1;
    protected String _direction; //Określa aktualny kierunek (zwrot) czołgu. Możliwe stany: left,right,down,up, początkowy right
    protected boolean _edge; //Określa, czy czołg dojechał do krawędzi mapy
    public Czolg(BufferedImage[] obr, int x, int y, int _hp)
    {
        super(obr,x,y);
        this._hp=_hp;
        _edge=false;
        
        
    }
    
     public void nadol()
     {
         if (this.getY()<=600)
         {
            this.move(0, _speed*_l);
            //dana klatka:
         }
         if (this.getY()>=601)
         {
             this.setY(601);
             _edge=true;
         }
         _direction="down";
         this.setAnimationFrame(2, 2);
         this.setAnimate(true);
     }
     
     public void dogory()
     {
         if (this.getY()>=1)
         {         
            this.move(0, -_speed*_l);
         }
         if (this.getY()<=1)
         {
             this.setY(1);
             _edge=true;
         }            
         _direction="up";
         this.setAnimationFrame(1, 1);
         this.setAnimate(true);
     }
     
     public void wlewo()
     {
         if (this.getX()>=1)
         {
            this.move(-_speed*_l, 0);
         }
         if (this.getX()<=1)
         {
             this.setX(1);
             _edge=true;
         }
         _direction="left";
         this.setAnimationFrame(3, 3);
         this.setAnimate(true);
     }
     
     public void wprawo()
     {
         if (this.getX()<=600)
         {
            this.move(_speed*_l, 0);
         }
         if (this.getX()>=601)
         {
             this.setX(601);
             _edge=true;
         }
         _direction="right";
            this.setAnimationFrame(0, 0);
            this.setAnimate(true);
     }
     public Pocisk strzal(BufferedImage[] obraz)
     {
         int x,y;
         x=(int)this.getX();
         y=(int)this.getY();
         switch (_direction)
         {
             case "up":
                 x=x+((50-16)/2)-1;
                 y=y-16;
                 break;
             case "down":
                 x=x+((50-16)/2)-2;
                 y=y+50;
                 break;
             case "left":
                 x=x-16;
                 y=y+((50-16)/2);
                 break;
             case "right":
                 x=x+50;
                 y=y+((50-16)/2);
                 break;    
                
         }
         Pocisk pocisk = new Pocisk(obraz,_direction,x,y);
         return pocisk;
     }
     
     @Override
     public void update(long _l) 
     {
        super.update(_l);
     }
     
    @Override
    public void render(Graphics2D gd) 
    {
        super.render(gd);
    }

    @Override
    public void move(double d, double d1) 
    {
        super.move(d, d1);
    }
}


class CzolgPrzeciwnika extends Czolg
{         
   private int _beforeX,_beforeY; 
   public CzolgPrzeciwnika( BufferedImage[] obr, int x, int y, int _hp, double speed)
    {
      super(obr, x, y,_hp);
        _l=10;
        _speed=speed;
        //_speed=0.4;
        _beforeX=0;
        _beforeY=0;
        this.changeDirection();
      
    }
   
   public void changeDirection()
   {
       int newDirection;
       newDirection=1 + (int)(Math.random() * 4);
       switch (newDirection)
       {
           case 1:
               _direction="left";
               break;
           case 2:
               _direction="right";
               break;
           case 3:
               _direction="up";
               break;
           case 4:
               _direction="down";
               break;
       }
   }
   
   public void collision(boolean coll)
   {
       if (coll)
       {
           this.changeDirection();
       }
   }
   
   @Override
   public void update (long _l)
   {
       super.update(_l);
       int ifShot=1 + (int)(Math.random() * 100);
       int ifChangeDir=1 + (int)(Math.random() * 400);
       switch (_direction)
       {
           case "up":
               this.dogory();
               break;
           case "down":
               this.nadol();
               break;
           case "left":
               this.wlewo();
               break;
           case "right":
               this.wprawo();
               break;
       }
       if ((_edge)||((_beforeX==(int)this.getX())&&(_beforeY==(int)this.getY()))||(ifChangeDir==159))
       {
           this.changeDirection();
           _edge=false;
       }
       _beforeX=(int)this.getX();
       _beforeY=(int)this.getY();
       if (ifShot==50)
       {
           Global.ENEMY_SHOT_G.add(this.strzal(Global.bulletImg));
       }
   }
}

class CzolgGracza extends Czolg
{
     public CzolgGracza( BufferedImage[] obr,BufferedImage[] serce1,BufferedImage[] serce2,BufferedImage[] serce3, int x, int y,int _hp)
     {
        super(obr, x, y,_hp); 
        _l=10;
        _speed=0.3;
        this.dogory();
        zycie1=new AnimatedSprite(serce1,650,100);
        zycie2=new AnimatedSprite(serce2,700,100);
        zycie3=new AnimatedSprite(serce3,750,100);
        

     } 

     
     
     @Override
     public void update(long _l)
     {
         super.update(_l);
         _hp-=Global.hpDecrease;
         Global.hpDecrease=0;

         if (_hp<0)
         {
             Global.gameOver=true;
         }

     }

     public void wyswietl(Graphics2D gd){
         if(_hp==3)
         {
         zycie1.render(gd);
         zycie2.render(gd);
         zycie3.render(gd);
         }
         if(_hp==2)
         {
         zycie1.render(gd);
         zycie2.render(gd);
         }
         if(_hp==1)
         {
         zycie1.render(gd);
         }
         
     }
       
   
}

class PlayerCollisionBullet extends BasicCollisionGroup 
{
    public PlayerCollisionBullet() 
    {
	pixelPerfectCollision = true;
    }
    @Override
   public void collided(Sprite s1, Sprite s2) 
   {
       Global.hpDecrease=1;
       s1.forceX(600);
       s1.forceY(600);
       s2.setActive(false);
       
       
   }
}